package com.junyuan.gsp.web;

import com.junyuan.gsp.bean.Result;
import com.junyuan.gsp.bean.Role;
import com.junyuan.gsp.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@RestController
public class RoleController
{
    @Autowired
    private RoleService roleService;

    @GetMapping("/roles/{id}")
    public Result findRoleById(@PathVariable Integer id)
    {
        Role role = roleService.findRoleById(id);
        return new Result(role);
    }

    @GetMapping("/roles")
    public Result findRoles(Integer page, Integer limit)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        return roleService.findRolesResult(map);
    }

    @DeleteMapping("/roles/{id}")
    public Result deleteRoleById(@PathVariable Integer id)
    {
        roleService.deleteRoleById(id);
        return Result.SUCCESS;
    }

    @PutMapping("/roles")
    public Result saveRole(Role role)
    {
        roleService.saveRole(role);
        return Result.SUCCESS;
    }

    @GetMapping("/roles/{id}/menus")
    public Result findMenusByRoleId(@PathVariable Integer id)
    {
        return roleService.findMenuResultByRoleId(id);
    }

    @PutMapping("/roles/{id}/menus")
    public Result saveMenus(@PathVariable Integer id, String menus)
    {
        roleService.updateMenus(id, menus);
        return Result.SUCCESS;
    }

    @GetMapping("/roles/{id}/access-policies")
    public Result findAccessPolicies(@PathVariable Integer id)
    {
        return roleService.findAccessPoliciesResultByRoleId(id);
    }

    @PutMapping("/roles/{id}/access-policies")
    public Result saveAccessPolicies(@PathVariable Integer id, String accessPolicies)
    {
        roleService.updateAccessPolicies(id, accessPolicies);
        return Result.SUCCESS;
    }

}
